package com.kbook.action.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.kbook.bean.UserBean;
import com.kbook.dao.CategoryDao;
import com.kbook.dao.ChapterDao;
import com.kbook.dao.NovelDao;
import com.kbook.dao.impl.CategoryDaoImpl;
import com.kbook.dao.impl.ChapterDaoImpl;
import com.kbook.dao.impl.NovelDaoImpl;
import com.kbook.log.LogWriter;
import com.kbook.tool.Constants;
import com.kbook.tool.Tools;
import com.kbook.vo.Category;
import com.kbook.vo.Chapter;
import com.kbook.vo.Novel;

public class ContentAction extends AbstractAction {
	//private static final String NOVEL_BASE_PATH="F:/novel";
	//private static final String NOVEL_BASE_PATH="/mnt/novel/novel/";
	private static final Logger logger = Logger.getLogger("ContentAction");
	
	@Override
	protected Object doAction(UserBean user, Map params) throws Exception {
		// TODO Auto-generated method stub
		Map resultMap = new HashMap();
		
		String novelId = (String)params.get("novelId");
		String chapterId = (String)params.get("chapterId");
		
		int int_novelId = Tools.parseInt(novelId);
		long long_chapterId = Tools.parseLong(chapterId);
		
		StringBuilder builder = new StringBuilder();
		if(int_novelId > Constants.NOVEL_FLAGER){
			builder.append(Constants.NOVEL_SEC_BASE_PATH)
			   .append("/").append(params.get("novelId"))
			   .append("/").append(params.get("chapterId"))
			   .append("/").append("1.txt");
		}else{
			builder.append(Constants.NOVEL_BASE_PATH)
			   .append("/").append(params.get("novelId"))
			   .append("/").append(params.get("chapterId"))
			   .append("/").append("1.txt");
		}
		
		
		//System.out.println(FileUtils.readFileToString(new File(builder.toString())));
		String content = Tools.replaceSpecialCharater(FileUtils.readFileToString(new File(builder.toString()), "gb2312"));
		//System.out.println(content);
		//System.out.println(result);
		
		NovelDao novelDao = new NovelDaoImpl();
		Novel novel = novelDao.query(int_novelId);
		
		ChapterDao chapterDao = new ChapterDaoImpl();
		Chapter chapter = chapterDao.query(int_novelId, long_chapterId);
		
		Chapter preChapter = chapterDao.queryPre(int_novelId, long_chapterId);
		Chapter nextChapter = chapterDao.queryNext(int_novelId, long_chapterId);
		
		CategoryDao categoryDao = new CategoryDaoImpl();
		Category category = categoryDao.query(novel.getCategoryId());
		
		resultMap.put("novel", novel);
		resultMap.put("chapter", chapter);
		resultMap.put("nextChapter", nextChapter);
		resultMap.put("preChapter", preChapter);
		resultMap.put("content", content);
		resultMap.put("category", category);
		
		return resultMap;
	}

	@Override
	protected void afterAction(Object obj, UserBean user, Map params) {
		// TODO Auto-generated method stub
		List<String> logParams = new ArrayList<String>();
		logParams.add((String)params.get("ip"));
		logParams.add((String)params.get("ua"));
		logParams.add((String)params.get("cid"));
		
		Map resultMap = (Map)obj;
		Category category = (Category)resultMap.get("category");
		logParams.add(category.getId()+"");
		logParams.add((String)params.get("novelId"));
		logParams.add((String)params.get("chapterId"));

		LogWriter.getInstance().createLog(logParams, logger);
	}

	
}
